#include <iostream>

using namespace std;

struct ListNode {
    int val;
    ListNode* next;
    ListNode() : val(0), next(nullptr) {}
    ListNode(int val) : val(val), next(nullptr) {}
    ListNode(int val, ListNode* next) : val(val), next(next) {}
};

void printList(ListNode* head) {
    if (head == nullptr) {
        cout << "null" << endl;
    }

    while (head != nullptr) {
        cout << head->val;
        if (head->next != nullptr) {
            cout << "->";
        }
        head = head->next;
    }

    cout << endl;
}

ListNode* createList(int arr[], int n) {
    if (n == 0) {
        return nullptr;
    }

    ListNode* head = new ListNode(arr[0]);
    ListNode* cur = head;
    for (int i = 1; i < n; i++) {
        cur->next = new ListNode(arr[i]);
        cur = cur->next;
    }

    return head;
}

class Solution {
public:
    ListNode* deleteDuplicated(ListNode* head) {
        if (head == nullptr) {
            return head;
        }

        ListNode* cur = head;
        while (cur->next != nullptr) {
            if (cur->val == cur->next->val) {
                cur->next = cur->next->next;
            } else {
                cur = cur->next;
            }
        }
        
        return head;
    }
};

int main() {
    int arr[] = {1, 1, 2, 2, 3, 4, 4, 5};
    ListNode* head = createList(arr, sizeof(arr) / sizeof(int));
    printList(head);

    head = Solution().deleteDuplicated(head);
    printList(head);

    return 0;
}